home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / ccmd / filelist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-19  |  20.8 KB  |  783 lines

  1. /*
  2.  Author: Howie Kaye
  3.  
  4.  Columbia University Center for Computing Activities, July 1986.
  5.  Copyright (C) 1986, 1987, Trustees of Columbia University in the City
  6.  of New York.  Permission is granted to any individual or institution
  7.  to use, copy, or redistribute this software so long as it is not sold
  8.  for profit, provided this copyright notice is retained.
  9. */
  10. /*
  11.  * Given a vector of directories, build a table of all files in those
  12.  * directories
  13.  * If the path passed here is the same as the previous one (help, and the 
  14.  * completion, uses previous listing for speed.
  15.  *
  16.  * All buffers are either malloc'ed or realloc'ed when needed.  If 
  17.  * these fail, bad things can happen -- i'm not yet sure what to do in this 
  18.  * case.
  19.  *
  20.  */
  21.  
  22. #include "ccmdlib.h"
  23. #include "filelist.h"
  24.  
  25. #ifdef NODIRLIB
  26. typedef struct {
  27.     int size;
  28.     struct direct *files;
  29.     struct direct *current;
  30. } DIR;
  31. #endif
  32.  
  33. #ifdef DIRENTLIB
  34. #define direct dirent            /* XXX kludge */
  35. #endif
  36.  
  37. #if unix
  38. #ifndef _CMUSR
  39. #include <pwd.h>            /* for "~" completion */
  40. #if SYSV
  41. struct passwd *getpwent(), *getpwnam(), *getpwuid();
  42. #endif /* SYSV */
  43. #endif /*  !_CMUSR */
  44. #endif /*  unix */
  45.  
  46. /* make sure toupper does what we want */
  47. #undef toupper
  48. #define toupper(c) (islower(c) ? (c)-'a'+'A' : (c))
  49. #define BUFLEN 120
  50.  
  51. char *tilde_expand();
  52.  
  53. char *namebuf;                /* hold filenames here */
  54. char *prevnamebuf;            /* previous name buffer */
  55.  
  56. dirfile *files;                /* list of files */
  57. dirfile *prevfiles;            /* list from previous pass */
  58.  
  59. dirfile **fileindex;            /* vector of files for sorting */
  60. dirfile **previndex;            /* index from previous pass */
  61.  
  62. int filecount;                /* number of files entries used */
  63. int prevfilecount;            /* from previous pass */
  64. int maxfiles;                /* number of file entries malloc'ed */
  65.  
  66. char *curfile;                /* current file in namelist */
  67. int namelen;                /* used length of namebuf */
  68. int maxnamelen;                /* malloc'ed length of namebuf */
  69. char **prevpath=NULL;            /* previous search path  */
  70. char **usepath=NULL;            /* expanded path to use */
  71. int currentpath= 0;            /* current spot in usepath */
  72. int usepathlen=0;
  73. int redofiles = TRUE;
  74. int *modtimes = NULL;
  75. int suffix_modtime = 0;
  76. char *oldsuffix = NULL;
  77.  
  78. char *malloc(), *realloc(), *index();
  79. void qsort();                /* quick sort routine */
  80.  
  81. int dirfilecmp();            /* predicate for qsort */
  82.  
  83. /*
  84.  * chng_suffix:
  85.  * determine whether or not suffix has changed.  if it has, need
  86.  * to reread the directory.
  87.  * if suffix and oldsuffix are not equal, or they are equal but the modtimes
  88.  * are different, return TRUE, else return FALSE
  89.  */
  90.  
  91. chng_suffix (suffix, oldsuffix)
  92. char *suffix, *oldsuffix;
  93. {
  94.   struct stat sbuf;
  95.  
  96.   if (strcmp (suffix, oldsuffix) != 0)
  97.     return (TRUE);
  98.   if (stat (suffix, &sbuf) != 0)    /* don't know, be safe */
  99.     return (TRUE);
  100.   if (suffix_modtime == sbuf.st_mtime)
  101.     return (FALSE);
  102.   suffix_modtime = sbuf.st_mtime;
  103.   return (TRUE);
  104. }
  105.  
  106.  
  107. /*
  108.  * cmfileini:
  109.  * (re)initialize file vars.  Useful in making new file appear for
  110.  * file parse.
  111.  */
  112. cmfileini()
  113. {
  114.     redofiles = TRUE;
  115. }
  116.  
  117. /*
  118.  * search_path:
  119.  * takes a NULL terminated vector of directories.
  120.  * returns a pointer to a listing of the files.
  121.  * this listing is guaranteed to stay intact until the function is
  122.  * called again.
  123.  * fcount is set to the number of files returned
  124.  */
  125.  
  126. dirfile **
  127. search_path(path,suffix,fcount) char **path; char *suffix; int *fcount; {
  128.   int pathlen;                /* length of the path */
  129.   char **p;
  130.   int i;
  131.   register char **p1, **p2;        /* as last time */
  132.   struct stat sbuf;
  133.   int *m,goodlen = 0;
  134.  
  135.   p1 = path;
  136.   p2 = prevpath;
  137.   m = modtimes;
  138.  
  139.   if ((suffix == NULL && oldsuffix == NULL) ||
  140.       (suffix != NULL && oldsuffix != NULL 
  141.        && !chng_suffix (suffix, oldsuffix)))
  142.       {
  143.       for (;!redofiles;) {        /* check if it is same path */
  144.       if (p1 == NULL || p2 == NULL) break; /* NULL list? */
  145.       if (*p1 == NULL && *p2 != NULL) break; /* one null? */
  146.       if (*p1 != NULL && *p2 == NULL) break;
  147.       if ((*p1 != NULL) && (*p2 != NULL))
  148.           if (strcmp(*p1,*p2))
  149.           break;        /* or different entry...rebuild list */
  150.       if (*p1 == NULL) {        /* end of list?  they are the same */
  151.                     /* same list as last time...so */
  152.           namebuf = prevnamebuf;    /* make everything point to last set*/
  153.           *fcount = filecount = prevfilecount;
  154.           files = prevfiles;
  155.           fileindex = previndex;
  156.           return(previndex);    /* and return prev index */
  157.       }
  158.       if (stat(*p1,&sbuf) != 0)    /* if a directory changed, rebuild */
  159.           break;
  160.       if (sbuf.st_mtime != *m) break;
  161.       p1++,p2++;                /* keep walking the list */
  162.       }
  163.   }
  164.   redofiles = FALSE;
  165.                     /* different path...free up old */
  166.                     /* buffers */
  167.  
  168.   if (prevnamebuf != NULL)        /* free old name buffer */
  169.     free(prevnamebuf);
  170.   if (prevfiles != NULL)        /* and old file list */
  171.     free(prevfiles);
  172.   if (previndex != NULL)        /* old index */
  173.     free(previndex);
  174.   if (prevpath != NULL) {        /* and old path */
  175.     for (p = prevpath; *p != NULL; p++)    /* have to free each name in path */
  176.       free(*p);
  177.     free(prevpath);
  178.   }
  179.   if (modtimes != NULL)
  180.       free(modtimes);
  181.   filecount = prevfilecount = 0;    /* no previous files */
  182.   fileindex = NULL;            /* or current files */
  183.   files = NULL;
  184.   namebuf = NULL;
  185.   curfile = NULL;
  186.   namelen = 0;
  187.   maxnamelen = 0;
  188.   maxfiles = 0;
  189.                     /* now copy the current path */
  190.   pathlen = 0;
  191.   for (p = path; *p != NULL; p++)    /* first find it's length */
  192.     pathlen++;
  193.   prevpath = (char **) malloc((pathlen+1)*sizeof(char *)); /* build vector */
  194.   modtimes = (int *) malloc((pathlen)*sizeof(int));
  195.   for (i = 0,p = path; *p != NULL; i++,p++) { /* then copy the names */
  196.     prevpath[i] = malloc((1+strlen(*p)) * sizeof(char));
  197.     strcpy(prevpath[i],*p);
  198.     if (stat(*p,&sbuf) == 0)
  199.     modtimes[i] = sbuf.st_mtime;
  200.     else modtimes[i] == -1;
  201.   }
  202.   prevpath[pathlen] = NULL;        /* NULL termination */
  203.  
  204.   if (usepath != NULL) {        /* and expanded path */
  205.     for (i = 0,p = usepath; i < usepathlen;p++,i++) {
  206.       if (*p != NULL) free(*p);
  207.     }
  208.     free(usepath);
  209.   }
  210.   usepath = (char **) malloc((pathlen+1)*sizeof(char *)); /* build vector */
  211.   usepathlen=pathlen;            /* and remember length */
  212.  
  213.   goodlen = 0;
  214.   for (i = 0; i < usepathlen; i++) {
  215.     int xlen = 0;
  216.  
  217.     if (suffix) 
  218.     xlen = strlen(suffix);
  219.                     /* if it starts with a /, use */
  220.     if (suffix && (suffix[0] == '/' || strlen(suffix) == 0 ||
  221.            suffix[0] == '~' || (strcmp(suffix,".") == 0) ||
  222.            (strcmp(suffix,"..") == 0))) {    
  223.       int j;                /* as an absolute path */
  224.       char *term = "";
  225.       if (suffix[strlen(suffix)-1] != '/')
  226.     term = "/";
  227.       usepath[i] = malloc(xlen + 2);
  228.       sprintf(usepath[i],"%s%s",suffix,"/");
  229.       goodlen = 1;
  230.       for (j = 1; j < usepathlen; j++)
  231.     usepath[j] = NULL;
  232.       break;
  233.     }
  234.     else {
  235.       usepath[i] = malloc((1+xlen+sizeof("/")+
  236.                strlen(prevpath[i]))*sizeof(char));
  237.       strcpy(usepath[i],prevpath[i]);
  238.       if (suffix) {
  239.     if (usepath[i][strlen(usepath[i])-1] != '/')
  240.         strcat(usepath[i], "/");
  241.     strcat(usepath[i],suffix);
  242.       }
  243.       if (stat(usepath[i],&sbuf) == 0)
  244.       goodlen++;
  245.     }
  246.   }
  247.   usepath[usepathlen] = NULL;
  248.   for (i =0; i < usepathlen; i++) {    /* now build up list */
  249.     currentpath = i;
  250.     if (usepath[i] != NULL && modtimes[i] != -1) 
  251.     search_dir(usepath[i],goodlen <= 1);
  252.   }
  253.                     /* build up index into list */
  254.   if (oldsuffix)
  255.       free(oldsuffix);
  256.   if (suffix) {
  257.       oldsuffix = malloc(strlen(suffix)+1);
  258.       strcpy(oldsuffix,suffix);
  259.   }
  260.   else oldsuffix = NULL;
  261.   fileindex = (dirfile **) malloc(sizeof(dirfile *) * filecount);
  262.   for (i = 0; i < filecount; i++) fileindex[i] = &files[i];
  263.                     /* and sort it */
  264.   qsort(fileindex,filecount,sizeof(dirfile *), dirfilecmp);
  265.  
  266.   previndex = fileindex;            /* save these for next time */
  267.   prevnamebuf = namebuf;
  268.   prevfiles = files;
  269.   *fcount = prevfilecount = filecount;
  270.   return(previndex);
  271. }
  272.  
  273. /*
  274.  * search_dir:
  275.  * builds up a file list for a single directory
  276.  * if a directory is wild, expands it.
  277.  * if directory is CWD, then includes ".", and ".."
  278.  */
  279.  
  280. DIR *opendir();
  281.  
  282. search_dir(dirname,showdot) char *dirname; int showdot; {
  283.   struct stat sbuf;
  284.   DIR *dirp, *dotdirp;            /* directory pointers */
  285.   register struct direct *d;        /* file entries in a dir */
  286.   struct direct *dotd, *readdir();
  287.   int i;
  288.   char *dirr=dirname;            /* actual directory name to use */
  289.   struct passwd *user;            /* passwd structure for "~" files */
  290.   int ispwd;                /* flag if looking at connect dir */
  291.   register dirfile *f;
  292.   int dirstrlen;
  293. #ifdef MSDOS
  294.   static char pwdbuf[51];
  295.   char *getcwd();
  296.   int ustrcmp();
  297. #endif
  298.   if (iswild(dirname)) {
  299.     expandwilddir(dirname);    
  300.     return;
  301.   }
  302. #if unix
  303.   if (dirname[0] == '~') {        /* "~user"? */ 
  304.     dirr = tilde_expand(dirname);    /* expand it */
  305.   }
  306. #endif
  307.   dirstrlen = strlen(dirr);
  308.   ispwd = !strcmp(dirr,".");        /* if looking at "." then pwd */
  309.  
  310.                     /* otherwise, we have to check */
  311.   if (stat(dirr,&sbuf) != 0)
  312.       return;
  313.   if (!((sbuf.st_mode & S_IFMT) == S_IFDIR)) {
  314.       return;
  315.   }
  316.   if (!ispwd) {
  317. #ifdef MSDOS
  318.     if (index(STRUCTTERM,dirr[strlen(dirr)-1])) { /* on msdos, */
  319.       char buf[70];            /* check if the directory is */
  320.       int unit;                /* the same as the connected */
  321.       union REGS sregs, oregs;        /* by doing a DOS call to get the */
  322.       struct SREGS segregs;        /* connected dir */
  323.       int i;
  324.       
  325.       segread(&segregs);        /* set segment registers */
  326.       unit = 0;                /* use default structure */
  327.       if (isalpha(*dirr))
  328.     unit = toupper(*dirr) - 'A' + 1; /* no use the specified struct */
  329.  
  330.       strcpy(buf,dirr);            /* copy the directory name */
  331.       strcat(buf,"/");            /* add a "/" */
  332.       sregs.h.ah = 0x47;        /* DOS int get cwd */
  333.       sregs.x.si = (int) &buf[strlen(buf)];
  334.       sregs.h.dl = unit;
  335.       intdosx(&sregs, &oregs, & segregs); /* do the interrupt */
  336.       for (i = 0; i < strlen(buf); i++)    /* and lowercase the string */
  337.     if (isupper(buf[i])) buf[i] = tolower(buf[i]);
  338.       dirp = opendir(buf);        /* open up directory */
  339.     }
  340.     else
  341.       dirp = opendir(dirr);        /* open directory we are looking at */
  342. #else
  343.     dirp = opendir(dirr);        /* open directory we are looking at */
  344. #endif
  345.     if (dirp == NULL) return;        /* can't...just return */
  346.     if (defstruct(dirr)) {        /* same structure? */
  347.       dotdirp = opendir(".");        /* open up "." */
  348.       if (dotdirp != NULL) {
  349.     d = readdir(dirp);        /* read dir entries */
  350.     dotd = readdir(dotdirp);
  351.     if (d != NULL && dotd != NULL)
  352. #if unix
  353.       ispwd = (d->d_ino == dotd->d_ino); /* are they the same? */
  354. #endif
  355. #ifdef MSDOS
  356.           ispwd = (!ustrcmp(d->d_name,getcwd(pwdbuf,50)));
  357. #endif
  358.     closedir(dotdirp);
  359.         rewinddir(dirp);
  360.       }
  361.     }                    /* if different struct, different */
  362.   }
  363.   else {
  364.     dirp = opendir(dirr);        /* open up directory */
  365.     if (dirp == NULL) return;        /* couldn't...go home */
  366.   }
  367.                     /* directory is now open */
  368.   if (!ispwd && !showdot) {        /* if not CWD, skip ".", ".." */
  369.       do d = readdir (dirp);
  370.       while (d && d->d_name[0] == '.' &&
  371.          (d->d_name[1] == 0 ||
  372.           (d->d_name[1] == '.' && d->d_name[2] == 0)));
  373.   }
  374.  
  375.   for (d = readdir(dirp); d != NULL; d = readdir(dirp)) {
  376.     int fnlength;
  377.                     /* do we need more files? */
  378.  
  379.     if (filecount == maxfiles) {
  380.       maxfiles += FILEINCR;
  381.       files =(dirfile *)cmrealloc(files, sizeof(dirfile)*(maxfiles));
  382.     }
  383.                     /* do we need more name space  */
  384. #if MSDOS || defined(NDIRLIB) || defined(DIRENTLIB)
  385.     fnlength = strlen(d->d_name);
  386. #else /* MSDOS */
  387. #if BSD
  388.     fnlength = d->d_namlen;
  389. #else
  390.     fnlength = strlen(d->d_name);
  391.     if (fnlength > 14)
  392.     fnlength = 14;
  393. #endif /* !BSD */
  394. #endif /* !MSDOS */
  395.     if (maxnamelen - (curfile - namebuf) < (fnlength + 1)) {
  396.       int offset = curfile - namebuf;
  397.       maxnamelen += NAMEINCR;
  398.       namebuf = cmrealloc(namebuf,sizeof(char)*maxnamelen);
  399.       curfile = namebuf + offset;
  400.     }
  401.     f = &files[filecount];        /* set up pointer to current file */
  402.     f->offset = curfile-namebuf;    /* it's offset into namebuf */
  403.  
  404.     strncpy(curfile, d->d_name, fnlength);
  405.     curfile[fnlength] = 0;
  406.     curfile += fnlength + 1;        /* bump current pointer by namelen */
  407.  
  408.     f->directory = dirname;        /* point to it's directory */
  409.     f->flags = 0;            /* no flags */
  410.     filecount++;
  411.   }
  412.   closedir(dirp);
  413. }
  414.   
  415.  
  416. /*
  417.  * dirfilecmp:
  418.  * predicate to compare two dirfile struct's for qsort
  419.  */
  420. dirfilecmp(a,b) register dirfile **a,**b; {
  421.   return(strcmp(&namebuf[(*a)->offset],&namebuf[(*b)->offset]));
  422. }
  423.  
  424.  
  425. /*
  426.  * ustrcmp:
  427.  * case insensitive string comparison
  428.  */
  429.  
  430. ustrcmp(s1,s2) char *s1, *s2; {
  431.   register char c1, c2;
  432.   for (;;s1++,s2++) {
  433.     c1 = toupper(*s1);
  434.     c2 = toupper(*s2);
  435.     if (c1 < c2) return(-1);
  436.     if (c1 > c2) return(1);
  437.     if (c1 == '\0') return(0);
  438.   }
  439. }
  440.  
  441. /* 
  442.  * expand the current directory, and place expansion in 
  443.  * usepath vector after current loc.
  444.  * expand usepath as we go.
  445.  */
  446.  
  447. expandwilddir(dirname) char *dirname; {
  448.   DIR *dirp;
  449.   struct direct *d;
  450.   static char basename[BUFLEN],fname[BUFLEN],rest[BUFLEN];
  451.   char c;
  452.   static char temp[BUFLEN];
  453.   
  454.   int i,base;
  455.   struct stat s;
  456.   
  457.   base = -1;                /* find the non wild base */
  458.   for (i = 0; i < strlen(dirname); i++) {
  459.     c=dirname[i];
  460.     if (index(DIRSEP,c)) {
  461.       base = i;
  462.       continue;
  463.     }
  464.     if (index(WILDCHARS,c)) break;
  465.   }
  466.   if (base == -1) {            /* no base dir.   use dot */
  467.     strcpy(basename,".");
  468.   }
  469.   else {                /* a base dir.  use it */
  470.     strncpy(temp,dirname,base);
  471.     temp[base] = '\0';
  472.     strcpy(basename,tilde_expand(temp));
  473.   }
  474.  
  475.   if (strlen(basename) == 0) strcpy(basename,"/");
  476.  
  477.   fname[0] = '\0';            /* the current wild part */
  478.   for (i = base+1; i < strlen(dirname); i++) {
  479.     if (index(DIRSEP,dirname[i])) break;
  480.     fname[i-(base+1)] = dirname[i];
  481.   }
  482.   fname[i-(base+1)] = '\0';
  483.  
  484.   i++;                    /* get past dirsep */
  485.   if (i < strlen(dirname))
  486.     strcpy(rest,&dirname[i]);        /* and the rest of the string */
  487.   else rest[0] =  '\0';
  488.   if (rest[strlen(rest)-1] == '/') rest[strlen(rest)-1] = '\0';
  489.  
  490.   if (strcmp(basename,"/") && strcmp(basename,".") && strcmp(basename,"..")) {
  491.                         /* we know these are dirs */
  492.     if (stat(basename,&s) == -1) {    /* and root breaks the msdos stat */
  493.       fprintf(stderr,"stat(%s) failed\n",basename); /* function */
  494.       return;
  495.     }
  496.     if (!((s.st_mode & S_IFMT) == S_IFDIR)) {
  497.       fprintf(stderr,"%s not a dir\n",basename);
  498.       return;
  499.     }
  500.   }
  501.   dirp = opendir(basename);
  502.   if (dirp == NULL) return;
  503.                     /* insert directories into path */
  504.   d = readdir(dirp);            /* kill off . and .. */
  505.   if (!strcmp(d->d_name,"."))
  506.     readdir(dirp);
  507.   else rewinddir(dirp);
  508.   for(d = readdir(dirp); d!= NULL; d = readdir(dirp)) {
  509.     if ((d->d_name[0] != '.') || (d->d_name[0] == '.' && fname[0] == '.')) {
  510.       if (fmatch(d->d_name,fname)) {    /* if we match the wildcard */
  511.     static char buf[BUFLEN];
  512.     if (!strcmp(basename,".")) 
  513.       basename[0] = '\0';
  514.     else {
  515.       strcpy(buf,basename);
  516.     }
  517.     if (strlen(buf) > 0)
  518.       if (strcmp(buf,"/")) strcat(buf,"/");
  519.     strcat(buf,d->d_name);
  520.     if (strcmp(buf,".") && strcmp(buf,"..")) {
  521.       if (stat(buf,&s) == -1) continue;
  522.       if (!((s.st_mode & S_IFMT) == S_IFDIR)) continue;
  523.     }
  524.     if (strlen(rest) > 0) {
  525.       strcat(buf,"/");
  526.       strcat(buf,rest);
  527.     }
  528.     addtopath(buf);            /* add to the path vector */
  529.       }
  530.     }
  531.   }
  532.   closedir(dirp);            /* all done */
  533. }
  534.  
  535. /*
  536.  * actually add directories to the path in use.
  537.  */
  538. addtopath(dirname) char *dirname; {
  539.   int i;
  540.  
  541.                     /* grow the path */
  542.   usepath =(char**) cmrealloc(usepath,(usepathlen+2)*sizeof(char *));
  543.                     /* move everything else down one */
  544.   for (i = usepathlen; i > currentpath; i--) {
  545.     usepath[i+1] = usepath[i];
  546.   }
  547.   usepathlen++;                /* make space for this string */
  548.   usepath[currentpath+1] = malloc(strlen(dirname)+1);
  549.   strcpy(usepath[currentpath+1],dirname); /* and copy it in */
  550. }
  551.  
  552. #ifdef notdef
  553. /*
  554.  * index function
  555.  * returns index of char c in string s
  556.  */
  557. char *
  558. index(sp,c)                /* get index into a string */
  559. register char *sp, c;
  560. {
  561.   for (; *sp && *sp != c; sp++);
  562.   if (*sp == '\0') return(NULL);
  563.   else return(sp);
  564. }
  565. #endif
  566.  
  567. /*
  568.  * routine to check if a string is wild
  569.  * match all chars in string against all wild chars
  570.  */
  571.  
  572. iswild(str) char *str; {
  573.   int hit[128],i;
  574.   int len1, len2;
  575.   for (i = 0; i < 128; i++) hit[i] = 0;    /* no hits yet */
  576.   len1 = strlen(str);
  577.   for(i = 0; i < len1; i++)        /* mark hits */
  578.     hit[str[i]] = 1;
  579.   len2 = strlen(WILDCHARS);
  580.   for (i = 0; i < len2; i++)        /* and check for them */
  581.     if (hit[WILDCHARS[i]]) return(TRUE);
  582.   return(FALSE);
  583. }
  584.  
  585. /*
  586.  * check if directory is on connected structure
  587.  */
  588. defstruct(dirname)
  589. char *dirname;
  590. {
  591. #ifdef MSDOS
  592.   int i;
  593.   char buf[100];
  594.   
  595.   getcwd(buf,100);            /* get connected dir */
  596.   for(i = 0; i < strlen(dirname); i++)
  597.     if (index(STRUCTTERM,dirname[i]))    /* a structure delimiter? */
  598.       return(toupper(dirname[i-1]) == toupper(buf[0]));
  599. #endif
  600.   return(TRUE);
  601. }
  602.  
  603.  
  604. #if unix
  605. /* 
  606.  * WHOAMI:
  607.  * 1) Get real uid
  608.  * 2) See if the $USER environment variable is set
  609.  * 3) If $USER's uid is the same as realuid, realname is $USER
  610.  * 4) Otherwise get logged in user's name
  611.  * 5) If that name has the same uid as the real uid realname is loginname
  612.  * 6) Otherwise, get a name for realuid from /etc/passwd
  613.  */
  614.  
  615. PASSEDSTATIC
  616. char *
  617. whoami () {
  618.   static char realname[256];        /* user's name */
  619.   static int realuid = -1;        /* user's real uid */
  620.   char loginname[256], envname[256];    /* temp storage */
  621.   char *getlogin(), *getenv(), *c;
  622.   struct passwd *p, *getpwnam(), *getpwuid(), *getpwent();
  623.  
  624.   if (realuid != -1)
  625.     return(realname);
  626.  
  627.   realuid = getuid ();            /* get our uid */
  628.  
  629.   /* how about $USER? */
  630.   if ((c = getenv("USER")) != NULL) {    /* check the env variable */
  631.     strcpy (envname, c);        
  632.     p = getpwnam(envname);
  633.     if (p->pw_uid == realuid) { /* get passwd entry */
  634.                     /* for envname */
  635.       strcpy (realname, envname);    /* if the uid's are the same */
  636.       return(realname);
  637.     }
  638.   }
  639.  
  640.   /* can we use loginname() ? */
  641.   if ((c =  getlogin()) != NULL) {    /* name from utmp file */
  642.     strcpy (loginname, c);    
  643.     if ((p = getpwnam(loginname)) != NULL) /* get passwd entry */
  644.       if (p->pw_uid == realuid) {    /* for loginname */ 
  645.     strcpy (realname, loginname);    /* if the uid's are the same */
  646.     return(realname);
  647.       }
  648.   }
  649.  
  650.   /* Use first name we get for realuid */
  651.   if ((p = getpwuid(realuid)) == NULL) { /* name for uid */
  652.     realname[0] = '\0';            /* no user name */
  653.     realuid = -1;
  654.     return(NULL);
  655.   }
  656.   strcpy (realname, p->pw_name);    
  657.   return(realname);
  658. }
  659. #endif
  660.  
  661. /*
  662.  * expand ~user to the user's home directory.
  663.  */
  664. char *
  665. tilde_expand(dirname) char *dirname; {
  666. #ifdef MSDOS
  667.   return(dirname);            /* no users in msdos */
  668. #endif /*  MSDOS */
  669. #if unix
  670.   struct passwd *user, *getpwuid(), *getpwnam();
  671.   static char olddir[BUFLEN];
  672.   static char oldrealdir[BUFLEN];
  673.   static char temp[BUFLEN];
  674.   int i;
  675.   char *whoami();
  676.  
  677.   if (dirname[0] != '~') return(dirname); /* not a tilde...return param */
  678.   if (!strcmp(olddir,dirname)) return(oldrealdir); /* same as last time. */
  679.                     /* so return old answer */
  680.   else {
  681.     for (i = 0; i < strlen(dirname); i++) /* find username part of string */
  682.       if (!index(DIRSEP,dirname[i]))
  683.     temp[i] = dirname[i];
  684.       else break;
  685.     temp[i] = '\0';            /* tie off with a NULL */
  686.     if (strlen(temp) == 1) {        /* if just a "~" */
  687.       user = getpwnam(whoami());    /*  get info on current user */
  688.     }
  689.     else {
  690.       user = getpwnam(&temp[1]);    /* otherwise on the specified user */
  691.     }
  692.   }
  693.   if (user != NULL) {            /* valid user? */
  694.     strcpy(olddir, dirname);        /* remember the directory */
  695.     strcpy(oldrealdir,user->pw_dir);    /* and their home directory */
  696.     strcat(oldrealdir,&dirname[i]);
  697.     return(oldrealdir);
  698.   }
  699.   else {                /* invalid? */
  700.     strcpy(olddir, dirname);        /* remember for next time */
  701.     strcpy(oldrealdir, dirname);
  702.     return(oldrealdir);
  703.   }
  704. #endif
  705. }
  706.  
  707. #ifdef notdef
  708. bcopy(src,dest,len)
  709. register char *src,*dest;
  710. register int len;
  711. {
  712.   if (src > dest)               /* Could source be overwritten? */
  713.     for(; len > 0; len--)       /* No */
  714.       *dest++ = *src++;         /* Simple forward copy */
  715.   else
  716.     for(; len >= 0; len--)       /* Yes */
  717.       *(dest+len) = *(src+len); /* Backwards copy */
  718. }
  719.  
  720. #endif
  721.  
  722. #ifdef NODIRLIB
  723.  
  724. DIR *
  725. opendir(name) {
  726.     DIR *dir;
  727.     int fd,open();
  728.     struct stat sbuf;
  729.     int x;
  730.  
  731.     if (stat(name,&sbuf) != 0) return(NULL);
  732.     if (!(sbuf.st_mode & S_IFDIR)) {
  733.     errno = ENOTDIR;
  734.     return(NULL);
  735.     }
  736.     fd = open(name,O_RDONLY,0);
  737.     if (fd == -1)
  738.     perror("open");
  739.     if (fd == -1) return(NULL);
  740.     if ((dir = (DIR *)malloc(sizeof(DIR))) == NULL) {
  741.     close(fd);
  742.     return(NULL);
  743.     }
  744.     dir->size = sbuf.st_size;
  745.     if ((dir->files = (struct direct *) malloc(dir->size)) == 0) {
  746.     close(fd);
  747.     free(dir);
  748.     return(NULL);
  749.     }
  750.     x = read(fd,dir->files,dir->size);
  751.     if (x == -1) perror("read");
  752.     dir->current = dir->files;
  753.     close(fd);
  754.     return(dir);
  755. }
  756.  
  757. struct direct *
  758. readdir(dirp) DIR *dirp; {
  759.     int s = dirp->size / sizeof(struct direct);
  760.     struct direct *temp;
  761.     if (dirp->current >= dirp->files + s)
  762.     return(NULL);
  763.     while (dirp->current->d_ino == 0)
  764.     if (dirp->current >= dirp->files + s)
  765.         return(NULL);
  766.     else dirp->current++;
  767.         if (dirp->current >= dirp->files + s)
  768.     return(NULL);
  769.     temp = dirp->current++;
  770.     return(temp);
  771. }
  772.     
  773. rewinddir(dirp) DIR *dirp; {
  774.     dirp->current = dirp->files;
  775. }
  776.  
  777. closedir(dirp) DIR *dirp;
  778. {
  779.     free(dirp->files);
  780.     free(dirp);
  781. }
  782. #endif
  783.